/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.saarh.interfaz.demo;

import com.ibm.icu.text.SimpleDateFormat;
import com.vaadin.terminal.FileResource;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Upload;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.ParseException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import jxl.*; 
import jxl.read.biff.BiffException;

/**
 *
 * @author Soma
 */
public class Uploader extends CustomComponent
                        implements Upload.SucceededListener,
                                   Upload.FailedListener,
                                   Upload.Receiver {

    Panel root;         // Root element for contained components.
    File  file;         // File to write to.

    public Uploader() {
        root = new Panel("My Upload Component");
        setCompositionRoot(root);

        // Create the Upload component.
        final Upload upload = new Upload("Upload the file here", this);

        // Use a custom button caption instead of plain "Upload".
        upload.setButtonCaption("Upload Now");

        // Listen for events regarding the success of upload.
        upload.addListener((Upload.SucceededListener) this);
        upload.addListener((Upload.FailedListener) this);

        root.addComponent(upload);
        root.addComponent(new Label("Click 'Browse' to select a file and then click 'Upload'."));
    }

    // Callback method to begin receiving the upload.
    @Override
    public OutputStream receiveUpload(String filename, String MIMEType) {
        FileOutputStream fos = null; // Output stream to write to
        file = new File("c:/" + filename);
        try {
            // Open the file for writing.
            fos = new FileOutputStream(file);
        } catch (final java.io.FileNotFoundException e) {
            return null;
        }

        return fos; // Return the output stream to write to
    }

    // This is called if the upload is finished.
    @Override
    public void uploadSucceeded(Upload.SucceededEvent event) {
        try {
            // Log the upload on screen. 
            Workbook workbook = Workbook.getWorkbook(new File("c:/"+event.getFilename()));
            
            Sheet sheet = workbook.getSheet(0);
            
            for(int r = 0; r<sheet.getRows(); r++){
                for(int c=0;c<sheet.getColumns();c++){ 
                    
                    if(sheet.getCell(c,r).getType().toString().equals("Date")){
                        
                        System.out.print( ((DateCell)sheet.getCell(c,r)).getDate() +"\t");
                        
                    }else{
                        
                        System.out.print(sheet.getCell(c,r).getContents()+"\t");
                        
                    }
                    
                }
                System.out.println("");
            }
             
        } catch (IOException ex) {
            root.addComponent(new Label("IOException:"+ex.getMessage()));
        } catch (BiffException ex) {
            root.addComponent(new Label("BiffException:"+ex.getMessage()));
        }
        //root.addComponent(new Label("File " + event.getFilename()+ " of type '" + event.getMIMEType()+"' uploaded."));
    }

    // This is called if the upload fails.
    @Override
    public void uploadFailed(Upload.FailedEvent event) {
        // Log the failure on screen.
        root.addComponent(new Label("Uploading " + event.getFilename() + " of type '" + event.getMIMEType() + "' failed."));
    }
}
